#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "../inc/data.h"
#include "../inc/general.h"
#include "../inc/myInfo.h"
#include "../inc/user_node.h"

void myInfo(user_Node_p currNode)
{
    while (1)
    {
        clearScreen();
        printf("                        我的信息\n");
        print_Lines3();
        printf("用户名:\t%s\n", currNode->data->username);
        printf("手机号:\t%s\n", currNode->data->phone);
        printf("余额:\t%d\n", currNode->data->money);
        print_Lines3();
        printf("指令: [0] 修改手机号 [1] 修改密码 [2] 充值 [3] 提现 [Q]返回主菜单\n");
        if (choice_myInfo(currNode) == 1)
        {
            return;
        }
    }
}

int choice_myInfo(user_Node_p currNode)
{
    char choice;
    while (1)
    {
        scanf(" %c", &choice);
        clearChar();
        switch (choice)
        {
        case '0':
            modify_phone(currNode);
            return 0;
        case '1':
            modify_password(currNode);
            return 0;
        case '2':
            deposit_money(currNode);
            return 0;
        case '3':
            withdraw_money(currNode);
            return 0;
        case 'Q':
        case 'q':
            return 1;
        default:
            printf("无效指令!请重新输入:");
            break;
        }
    }
}

void modify_phone(user_Node_p currNode)
{
    char newPhone[12];
    printf("请输入新的手机号(11位): ");
    scanf(" %11s", newPhone);
    clearChar();

    // 简单的手机号验证
    if (strlen(newPhone) != 11)
    {
        printf("手机号必须为11位!\n");
        waitForAnyKey();
        return;
    }

    if (is_valid_phone(newPhone) == 0)
    {
        printf("无效的手机号！\n");
        printf("要求:11位数字,有效号段(13/14/15/16/17/18/19开头)\n");
        waitForAnyKey();
        return;
    }

    strcpy(currNode->data->phone, newPhone);
    printf("手机号修改成功!\n");
    waitForAnyKey();
}

void modify_password(user_Node_p currNode)
{
    char oldPass[20], newPass[20], confirmPass[20];

    printf("请输入旧密码: ");
    scanf(" %19s", oldPass);
    clearChar();

    if (strcmp(currNode->data->password, oldPass))
    {
        printf("旧密码不正确!\n");
        waitForAnyKey();
        return;
    }

    printf("请输入新密码: ");
    scanf("%19s", newPass);
    clearChar();

    printf("请确认新密码: ");
    scanf("%19s", confirmPass);
    clearChar();

    if (strcmp(newPass, confirmPass) != 0)
    {
        printf("两次输入的新密码不一致!\n");
        waitForAnyKey();
        return;
    }

    strcpy(currNode->data->password, newPass);
    printf("密码修改成功!\n");
    waitForAnyKey();
}

void deposit_money(user_Node_p currNode)
{
    int amount;
    printf("请输入充值金额(正整数): ");
    if (scanf(" %d", &amount) != 1 || amount <= 0)
    {
        printf("请输入有效的正整数金额!\n");
        clearChar();
        waitForAnyKey();
        return;
    }
    clearChar();
    if (amount > 10000)
    {
        printf("单次充值金额不能超过10000!\n");
        return;
    }
    currNode->data->money += amount;
    printf("充值成功! 当前余额: %d\n", currNode->data->money);
    waitForAnyKey();
}

void withdraw_money(user_Node_p currNode)
{
    int amount;
    printf("请输入提现金额(正整数): ");
    if (scanf(" %d", &amount) != 1 || amount <= 0)
    {
        printf("请输入有效的正整数金额!\n");
        clearChar();
        waitForAnyKey();
        return;
    }
    getchar(); // 清除输入缓冲区

    if (amount > 10000)
    {
        printf("单次提现金额不能超过10000!\n");
        return;
    }
    if (amount > currNode->data->money)
    {
        printf("余额不足! 当前余额: %d\n", currNode->data->money);
        waitForAnyKey();
        return;
    }

    currNode->data->money -= amount;
    printf("提现成功! 当前余额: %d\n", currNode->data->money);
    waitForAnyKey();
}
